package com.example.demo.controller;

import com.example.demo.entity.Purchases;
import com.example.demo.service.PurchasesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/purchases")
public class PurchasesController {

    @Autowired
    private PurchasesService purchasesService;

    @GetMapping
    public List<Purchases> getAllPurchases() {
        return purchasesService.list();
    }

    @GetMapping("/{id}")
    public Purchases getPurchaseById(@PathVariable Integer id) {
        return purchasesService.getById(id);
    }

    @PostMapping
    public boolean createPurchase(@RequestBody Purchases purchase) {
        return purchasesService.save(purchase);
    }

    @PutMapping("/{id}")
    public boolean updatePurchase(@PathVariable Integer id, @RequestBody Purchases purchase) {
        purchase.setPurchaseId(id); // 假设实体中有setPurchaseId方法
        return purchasesService.updateById(purchase);
    }

    @DeleteMapping("/{id}")
    public boolean deletePurchase(@PathVariable Integer id) {
        return purchasesService.removeById(id);
    }
}
